package com.tencent.could.ocrdemo.demo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.ahrykj.weddingcartaxi.R;
import com.mob.MobSDK;
import com.tencent.could.ocrdemo.utils.AuthService;
import com.tencent.could.ocrdemo.utils.CommonUtil;
import com.tencent.could.ocrdemo.utils.CustomConfigUtil;
import com.tencent.could.ocrdemo.utils.FileUtil;

import java.io.File;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.wechat.friends.Wechat;

import static com.tencent.could.ocrdemo.demo.ResourcesManager.IMAGE_TEST_URL;

public class MainActivity extends AppCompatActivity {

    Button btnTake;
    Button btnSelect, btn_save, btn_shar, btn_shar_pdf;
    ImageView ivShow;
    Button btnrh;
    Bitmap bitmap = null;
    File photoFile;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what == 1) {
//                Toast.makeText(MainActivity.this, message.obj.toString(), Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnTake = (Button) findViewById(R.id.btn_take);
        btnSelect = (Button) findViewById(R.id.btn_select);
        ivShow = (ImageView) findViewById(R.id.iv_show);
        btnrh = (Button) findViewById(R.id.btn_rh);
        btn_save = (Button) findViewById(R.id.btn_save);

        btn_shar = (Button) findViewById(R.id.btn_shar);
        btn_shar_pdf = (Button) findViewById(R.id.btn_shar_pdf);


        photoFile = new File(getExternalFilesDir("img"), "scan.jpg");

        btnTake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(CropActivity.getJumpIntent(MainActivity.this, false, photoFile), 100);
            }
        });

        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // AuthService.getAuth(handler);
                startActivityForResult(CropActivity.getJumpIntent(MainActivity.this, true, photoFile), 100);
            }
        });
        btnrh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (bitmap != null) {
                    ivShow.setImageBitmap(CustomConfigUtil.getInstance().sharpenImageAmeliorate(bitmap));
                } else {
                    Toast.makeText(MainActivity.this, "请先选择图片或者拍照证件", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (bitmap != null) {
                    CommonUtil.saveBitmap2file(bitmap, getApplicationContext());
                    Toast.makeText(MainActivity.this, "图片保存成功，请在相册查看", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MainActivity.this, "请先选择图片或者拍照证件", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn_shar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bitmap != null) {
                    OnekeyShare oks = new OnekeyShare();
// 参考代码配置章节，设置分享参数
//                    oks.setImageData(bitmap);
                    oks.setFilePath(photoFile.getPath());

//通过OneKeyShareCallback来修改不同平台分享的内容
                    oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
                        @Override
                        public void onShare(Platform platform, Platform.ShareParams shareParams) {

                        }
                    });
                    oks.show(MainActivity.this);
                } else {
                    Toast.makeText(MainActivity.this, "请先选择图片或者拍照证件", Toast.LENGTH_SHORT).show();
                }

            }
        });

        btn_shar_pdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (bitmap != null) {
                    Platform platform = ShareSDK.getPlatform(Wechat.NAME);
                    Platform.ShareParams shareParams = new Platform.ShareParams();
                    shareParams.setFilePath(FileUtil.saveBitmapForPdf(bitmap, com.rykj.util.FileUtil.IMAGEPATH, "zj.pdf").getPath());
                    shareParams.setImageUrl(IMAGE_TEST_URL);
                    shareParams.setText(ResourcesManager.getInstace(MobSDK.getContext()).getText());
                    shareParams.setTitle(ResourcesManager.getInstace(MobSDK.getContext()).getTitle());
                    shareParams.setShareType(Platform.SHARE_FILE);
                    platform.setPlatformActionListener(new PlatformActionListener() {
                        @Override
                        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

                        }

                        @Override
                        public void onError(Platform platform, int i, Throwable throwable) {

                        }

                        @Override
                        public void onCancel(Platform platform, int i) {

                        }
                    });
                    platform.share(shareParams);
                } else {
                    Toast.makeText(MainActivity.this, "请先选择图片或者拍照证件", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 100 && photoFile.exists()) {
            bitmap = BitmapFactory.decodeFile(photoFile.getPath());
            ivShow.setImageBitmap(bitmap);
        }
    }
}
